package com.poker;

import org.springframework.util.CollectionUtils;

import java.util.List;

public class Gambler implements Comparable {
    /**
     * 赌徒名称
     */
    private String name;
    /**
     * 手牌
     */
    private List<Integer> pokers;

    private PokerTypeEnum pokerType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 牌从大到小排序
     */
    public List<Integer> getPokers() {
        if (!CollectionUtils.isEmpty(pokers)) {
            pokers.sort((p1, p2) -> {
                return p2.compareTo(p1);
            });
        }
        return pokers;
    }

    public void setPokers(List<Integer> pokers) {
        this.pokers = pokers;
    }

    /**
     * 比较获取手上的牌型
     * 算法：循环比较每张牌，看有几张是一样的，取最多的一样的为几条：
     * 最多两张i一样为对子，
     * 最多3张一样为三条
     * 最多四张一样的为四条
     * 五张都不一样，但是第一张和最后一张相差等于4，那么是顺子，例如：2，3，4，5，6
     */
    public PokerTypeEnum getPokerType() {
        if (!CollectionUtils.isEmpty(pokers)) {
            int maxSame = 1; //最多几张一样
            List<Integer> pokerList = getPokers();
            for (int i = 0; i < 5; i++) {
                int same = 1;// 当前这张牌有几张一样的
                Integer poke1 = pokerList.get(i);
                for (int j = i + 1; j < 5; j++) {
                    Integer poke2 = pokerList.get(j);
                    if (poke1.intValue() == poke2.intValue()) {
                        same += 1;
                    }
                }
                if (maxSame < same) {
                    maxSame = same;
                }
            }
            switch (maxSame) {
                case 2:
                    return PokerTypeEnum.DUI_ZI;
                case 3:
                    return PokerTypeEnum.SAN_TIAO;
                case 4:
                    return PokerTypeEnum.SI_TIAO;
                case 1:
                    if (pokerList.get(0) - pokerList.get(4) == 4) {
                        return PokerTypeEnum.SHUN_ZI;
                    } else {
                        return PokerTypeEnum.ZA_PAI;
                    }
                default:
                    return PokerTypeEnum.ZA_PAI;
            }
        }
        return pokerType;
    }

    public void setPokerType(PokerTypeEnum pokerType) {
        this.pokerType = pokerType;
    }

    /**
     * 比较手牌大小：
     * 1、根据枚举值大小比较
     * 2、枚举值一样时，采用个枚举值自己的比较规则
     * 3、暂时只实现杂牌和四条的比较规则（因为简单。。。）
     */
    @Override
    public int compareTo(Object o) {
        if (o instanceof Gambler) {
            int i = 0;
            if (null != getPokerType() && null != ((Gambler) o).getPokerType()) {
                i = getPokerType().getBackValue().compareTo(((Gambler) o).getPokerType().getBackValue());
            }
            if (i != 0) {
                return i;
            } else {
                switch (getPokerType()) {
                    case ZA_PAI:
                        PokerCompare zaPai = new ZaPaiComparePokers();
                        return zaPai.comparePokers(getPokers(), ((Gambler) o).getPokers());
                    case SI_TIAO:
                        PokerCompare siTiao = new SiTiaoComparePokers();
                        return siTiao.comparePokers(getPokers(), ((Gambler) o).getPokers());
                    default:
                        throw new RuntimeException("not implements!!!");
                }

            }
        } else {
            throw new RuntimeException("compare type is not the same");
        }
    }
}
